package com.xuyh.pdf.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

/**
 * @author xuyh
 * @email 497418972@qq.com
 * @data 2019年3月21日12:39:27
 * @description 合并PDF
 */
public class Photo2PDF {
    /**
     * 合并输出文件名称
     */
    private final static String MERGE_OUTPUT_FILE = "D:\\pdf\\%s.pdf";
    /**
     * 合并PDF的文件
     */
    private final static String MERGE_PHOTO_PATH = "D:\\pdf\\photo1";


    public static void main(String[] args) {
        mergePdf(pdfFilePath());
    }


    public static TreeMap<Integer, String> pdfFilePath() {
        TreeMap<Integer, String> map = new TreeMap<>(Integer::compareTo);
        File file = new File(MERGE_PHOTO_PATH);
        File[] files = file.listFiles();
        assert files != null;
        for (File f : files) {
            String name = f.getName();
            String[] split = name.split("_");
            name = split[split.length - 1];
            int indexOf = name.indexOf(".");
            String substring = name.substring(0, indexOf);
            Integer index = Integer.valueOf(substring);
            map.put(index, f.getAbsolutePath());
        }
        return map;
    }

    public static void mergePdf(TreeMap<Integer, String> map) {
        Document document = null;

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(String.format(MERGE_OUTPUT_FILE, System.currentTimeMillis()));
            document = new Document();
            PdfWriter.getInstance(document, fos);
            document.open();
            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            float totalWidth = table.getTotalWidth();
            float totalHeight = table.getTotalHeight();
            for (Integer index : map.keySet()) {
                String path = map.get(index);
                BufferedImage bufferedImage = ImageIO.read(new FileInputStream(path));
                bufferedImage = imgColorGradation(bufferedImage, 60, 60, 60);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                assert bufferedImage != null;
                ImageIO.write(bufferedImage, "jpg", bos);
                Image image = Image.getInstance(bos.toByteArray());
                bos.close();
                image.scaleToFit(totalWidth, totalHeight);
                PdfPCell cell = new PdfPCell();
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBorderWidth(0);
                cell.setImage(image);
                cell.setRotation(0);
                table.addCell(cell);
            }
            document.add(table);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (document != null) {
                    document.close();
                }
                if (fos != null) {
                    fos.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static BufferedImage imgColorGradation(BufferedImage imgSrc, int r, int g, int b) {
        try {
            //创建一个不带透明度的图片
            BufferedImage back = new BufferedImage(imgSrc.getWidth(), imgSrc.getHeight(), BufferedImage.TYPE_INT_RGB);
            int width = imgSrc.getWidth();
            int height = imgSrc.getHeight();
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    int pixel = imgSrc.getRGB(j, i);
                    Color color = new Color(pixel);
                    int red = color.getRed() + r;
                    if (red > 255) {
                        red = 255;
                    }
                    if (red < 0) {
                        red = 0;
                    }
                    int green = color.getGreen() + g;
                    if (green > 255) {
                        green = 255;
                    }
                    if (green < 0) {
                        green = 0;
                    }
                    int blue = color.getBlue() + b;
                    if (blue > 255) {
                        blue = 255;
                    }
                    if (blue < 0) {
                        blue = 0;
                    }
                    color = new Color(red, green, blue);
                    int x = color.getRGB();
                    back.setRGB(j, i, x);
                }
            }
            return back;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
